var cityName = [];
$(document).ready(function () {
	getOrders();

	var flag = true;


	$('#addMember').click(function() {
		//$('#addm').css('display','block');
		//return false;
		if(flag) {
			flag = false;
			var str = '<form action="javascript:;" method="bpost" id="addNew" class="form-horizontal" role="form"><div class="form-group" style="text-align:center;font-size:16px;margin:0px;"><div class="alert alert-warning"style="margin:0px 0px 7px 0px;;">请填写要添加的新员工信息(<span style="color:red">*</span>为必填项)</div></div><div class="form-group"><label class="col-sm-3 control-label" for="username">登陆名:<span style="color:red;">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="username" name="username" required pattern="^[a-zA-Z0-9_]{2,36}$" placeholder="登陆名"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="phone">电话:<span style="color:red">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="phone" name="phone" required maxlength="11" pattern="^1[0-9]{10}$" placeholder="电话"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="realname">真实姓名:<span style="color:red">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="realname" name="realname" placeholder="真实姓名" required></div></div><div class="form-group"><label class="col-sm-3 control-label" for="title">职称:</label><div class="col-sm-7"><input type="text" class="form-control" id="title" name="title" placeholder="职称"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="leader">直接领导:</label><div class="col-sm-7"><input type="text" class="form-control" id="leader" name="leader" placeholder="直接领导"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="startTime">入职时间:</label><div class="col-sm-7"><input type="text" class="form-control" id="startTime" name="startTime" onfocus="WdatePicker({minDate:&quot;2015-01-01&quot;})" onkeydown="return!1" autocomplete="false"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="endTime">离职时间:</label><div class="col-sm-7"><input type="text" class="form-control" id="endTime" name="endTime" onfocus="WdatePicker({minDate:&quot;2015-01-01&quot;})" onkeydown="return!1" autocomplete="false"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="city">所属城市:</label><div class="col-sm-7"><select name="city" style="overflow:auto" id="city" class="form-control" required><option value="1"disabled>获取中。。。</option></select></div></div><div class="form-group"><label class="col-sm-3 control-label" for="city">工种:<span style="color:red">*</span></label><div class="col-sm-7"><label class="checkbox-inline"><input type="checkbox" name="workType[]" value="0">收派员</label><label class="checkbox-inline"><input type="checkbox" name="workType[]" value="1">集结点</label><label class="checkbox-inline"><input type="checkbox" name="workType[]" value="2">支线</label><label class="checkbox-inline"><input type="checkbox" name="workType[]" value="3">BD</label></div></div><div class="form-group"><input type="submit" name="submit" value="添加" id="doAdd2"class="btn btn-default"style="display:none;"></div></form>';

			$('#myModal #myModalLabel').html('添加新员工');
			$('#myModal .modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal" id="myModalClose">取消</button><button type="button" class="btn btn-primary" id="doAdd"mrmId="'+$(this).parents('tr').attr('mrmId')+'"mrname="'+$(this).attr('mname')+'">添加</button>');
			$('#myModal .modal-body').html(str);
			$('#myModal').modal('show');
			if(!cityName.length) {
				$.ajax({
					type: 'post', url: SITE_PATH + 'admin/hr/getCity',
					success: function (msg) {
						flag = true;
						if(msg['result']) {
							var str = '';
							cityName = msg['city'];
							for (var k in cityName)
								str += "<option value=" + cityName[k]['id'] + ">" + cityName[k]['name'] + "</option>";
							$('#city').html(str);
						}
					},
					error:function() {
						alert('获取城市信息失败');
						flag = true;
					}
				})
			}else {
				flag = true;
				var str = '';
				for (var k in cityName)
					str += "<option value=" + cityName[k]['id'] + ">" + cityName[k]['name'] + "</option>";
				$('#city').html(str);
			}

		}
	});

	$('input[name="btn"]').click(function() {
		$('#addm').css('display','none');
	});
	$('#myModal').on('click','#doAdd', function() {
		$('#doAdd2').click();
	});

	$('#myModal').on('submit', '#addNew',function(){
		//$('#addm').css('display','none');
		$.ajax({
			type:'post',
			url:SITE_PATH+'admin/hr/add',
			data:$('#addNew').serialize(),
			timeout:10000,
			success:function (j) {
				if(j['result']) {
					alert('添加成功');
					$('.close').click();
					getOrders();
				}else
					alert('提示：'+j['msg']) ;
			},
			error:function() {
				alert('操作出错');
			}
		})
	});


	$('.showpage').on('click','a',function() {
		if($(this).attr('page').trim() == $('.current').html().trim()) {
			return false;
		}
		getOrders($(this).attr('page').trim() );
	})



});
$('#toSearch').click(function(){
	getOrders();
});
$('#toReset').click(function(){
	$('#workerName').val('');
	getOrders();
})

function getOrders(p) {
	p = p || 1;
	let turl = SITE_PATH+'admin/hr/workersGet?page='+p;
	if($('#workerName').val()) {
		turl += '&search='+$('#workerName').val();
	}
	$.ajax({
		type:'post',
		url:turl,
		success:function(j) {
			if(j.result==1) {
				var listHtml = '';
				for (var k in j.workers) {
					var worker = j.workers[k];
					listHtml += '<tr><td>' + worker.id + '</td><td>' + worker.no + '</td><td>' + worker.user + '</td><td>' + worker.pwd_state + '</td><td>' + worker.phone + '</td><td class="realname">' + worker.realname + '</td><td>' + worker.time_add + '</td><td>入职时间</td><td>试用期截止</td><td>直接领导</td><td>' + worker.title + '</td><td>' + worker.workerType + '</td><td>' + worker.injob + '</td><td>' + worker.city + '</td><td>' + worker.rbutton + '</td></tr>';
				}
				$('#listTBody').html(listHtml);
				$('.showpage').html(j['page']);
				$('#workerName').val(j.searchRN);
			}else {
				alert(j.msg);
			}

		}
	});
}

$('.table').on('click', '.resetPwd', function() {
	var rname = $(this).parents('tr').find('.realname').html();
	if(window.confirm(rname+'，确定要重置密码!')) {
		$.ajax({
			type: 'post',
			url: SITE_PATH + 'admin/hr/ressetPwd',
			data: {'uid':$(this).attr('uid')},
			timeout: 3000,
			success: function (d) {
				d['result'] ? alert('重置该用户密码成功') : alert('提示：' + d['msg']);
			},
			error:function() {
				alert('重置操作失败');
			}

		});
	}
})
$('.table').on('click', '.toEdit', function() {
	$.ajax({
		type: 'post',
		url: SITE_PATH + 'admin/hr/toEditWorktype',
		data: {'uid':$(this).attr('uid')},
		timeout: 3000,
		success: function (d) {
			if(!d['worker']['id']) return false;
			var arr = [];
			for(var i in d['workType']) {
				arr.push(+d['workType'][i]['type']);
			}

			var str = '<form action="javascript:;" method="bpost" id="editform" class="form-horizontal" role="form"><div class="form-group"><label class="col-sm-3 control-label" for="id">用户ID:<span style="color:red">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="id" name="id"value="'+d['worker']['id']+'" readonly></div></div><div class="form-group"><label class="col-sm-3 control-label" for="username">登陆名:<span style="color:red">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="username"name="username"value="'+d['worker']['name']+'"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="realname">真实姓名:<span style="color:red">*</span></label><div class="col-sm-7"><input type="text" class="form-control" id="realname"name="realname"value="'+d['worker']['realname']+'"></div></div><div class="form-group"><label class="col-sm-3 control-label" for="mycity">所属城市:</label><div class="col-sm-7"><select name="city" style="overflow:auto" id="mycity"name="mycity" class="form-control" required><option value="1"disabled>获取中。。。</option></select></div></div><div class="form-group"><label class="col-sm-3 control-label" for="city">工种:</label><div class="col-sm-7"><label class="checkbox-inline"><input type="checkbox" name="wtype[]" value="0"'+($.inArray(0,arr)!=-1 ? 'checked':'')
					+'>收派员</label><label class="checkbox-inline"><input type="checkbox" name="wtype[]" value="1"'+($.inArray(1,arr)!=-1 ? 'checked':'')
					+'>集结点</label><label class="checkbox-inline"><input type="checkbox" name="wtype[]" value="2"'+($.inArray(2,arr)!=-1 ? 'checked':'')
					+'>支线</label><label class="checkbox-inline"><input type="checkbox" name="wtype[]" value="3"'+($.inArray(3,arr)!=-1 ? 'checked':'')
					+'>BD</label></div></div><input type="hidden"name="oldWorkType"valeu="'+arr.join(',')+'"/><input type="submit" name="submit"  id="doSave2"class="btn btn-default"style="display:none;"></form>';

			$('#myModal2 #myModalLabel').html('编辑员工');
			$('#myModal2 .modal-footer').html('<button type="button" class="btn btn-default" data-dismiss="modal" id="myModalClose2">取消</button><button type="button" class="btn btn-primary" id="doSave"mrmId="'+$(this).parents('tr').attr('mrmId')+'"mrname="'+$(this).attr('mname')+'">保存</button>');
			$('#myModal2 .modal-body').html(str);

			if(!cityName.length) {
				$.ajax({
					type: 'post', url: SITE_PATH + 'admin/hr/getCity',
					success: function (msg) {
						flag = true;
						if(msg['result']) {
							var str = '';
							cityName = msg['city'];
							for (var k in cityName) {
								str += '<option value="' + cityName[k]['id'] + '"' + ((+cityName[k]['id']) == d['worker']['city_id'] ? 'selected' : '') + ">" + cityName[k]['name'] + "</option>";
							}
							$('#mycity').html(str);
						}
					},
					error:function() {
						alert('获取城市信息失败');
						flag = true;
					}
				})
			}else {
				flag = true;
				var str = '';
				for (var k in cityName)
					str += '<option value="' + cityName[k]['id'] +'"'+((+cityName[k]['id'])==d['worker']['city_id']?'selected':'') + ">" + cityName[k]['name'] + "</option>";
				$('#mycity').html(str);
			}

			$('#myModal2').modal('show');

		},
		error:function() {
		}

	});
})

$('#myModal2').on('click','#doSave', function() {
	$('#doSave2').click();
});

$('#myModal2').on('submit', '#editform',function(){
	$.ajax({
		type:'post',
		url:SITE_PATH+'admin/hr/doEditWorktype',
		data:$('#editform').serialize(),
		timeout:5000,
		success:function (j) {
			console.log(j);
			if(j['result']) {
				alert('编辑成功');
				$('.close').click();
				getOrders($('.current').html());
			}else
				alert('提示：'+j['msg']) ;
		},
		error:function() {
			alert('操作出错或超时');
		}
	})
});


